const util = require('../../../utils/util.js')
import {
  list
} from '../../../api/virtualStock'
import {
  getInfo
} from '../../../api/mch'
import { findByWater } from '../../../api/bom'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageNum: 1,
    pageSize: 1,
    isShow: false,
    length: '',
    mch: {},
    waters: [],
    tabbarHeight: app.globalData.tabbarHeight,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this
    // this.loadProduct()
    // this.loadMch()
    // this.gettabBarHeight()
    if (util.isNull(app.globalData.token)) {
      wx.navigateTo({
        url: '/pages/login/index',
      })
    }
  },
  onShow: function () {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 1
      })
    }
    if (util.isNull(app.globalData.token)) {
      // wx.navigateTo({
      //   url: '/pages/login/index',
      // })
    }
     else {
      this.loadMch()
      this.loadProduct()
    }
  },

  loadMch() {
    var _this = this;
    getInfo(app.globalData.mchId).then(res => {
      _this.data.mch = res.data;
      _this.setData({
        mch: _this.data.mch
      })
    });
  },
  usetransfer(e) {
    wx.navigateTo({
      url: '/pages/member/waterTransfer/index?virtualStockId=' + e.currentTarget.dataset.id,
    })
  },
  toIndex() {
    if (util.isNull(app.globalData.token)) {
      wx.navigateTo({
        url: '/pages/login/index',
      })
      return
    }
    wx.switchTab({
      url: '/pages/index/index'
    })
  },
  loadProduct() {
    list({
      subType: '2'
    }).then(res => {
      this.data.waters = res.data.data
      this.setData({
        waters: this.data.waters
      })
    })
  },
  useCoupon(e) {
    this.gettabBarHeight()
    this.data.isShow = true
    findByWater({
      productId: e.currentTarget.dataset.id
    }).then(res => {
      this.selectComponent('#bottomFrame').showFrame(res.data.productId, "buyNow");
    })
    this.setData({
      isShow: this.data.isShow
    })
  },
  gettabBarHeight: function () {
    // wx.getSystemInfo({success(res){
    //   let lift = res.screenHeight-res.safeArea.bottom
    //   let tabbarHeight = Number(lift) +50
    //   console.log(tabbarHeight,'tabbarHeight')
    //   wx.setStorageSync('tabarHeight', tabbarHeight)
    // }})
    var tabBarType = 1
    wx.setStorageSync('tabBarType', tabBarType)
  }

  // onPullDownRefresh() {
  //   wx.showNavigationBarLoading()
  //   setTimeout(() => {
  //     wx.hideNavigationBarLoading()
  //     this.selectComponent('#products').downRefresh();
  //     wx.stopPullDownRefresh()
  //   }, 1000);
  // },
  // onReachBottom() {
  //   if(this.data.componentsList.length>0){
  //     if (this.data.componentsList[this.data.length - 1].name == 'products') {
  //       this.selectComponent('#products').onReachBottom();
  //      }else if(this.data.componentsList[this.data.length - 1].name == 'productRow'){
  //        this.selectComponent('#productRow').onReachBottom();
  //      }
  //   }
  // },
})